<?php

$plugin = array(
    'version' => '0.2',
    'author' => 'Mykhailo Kotsur',
    'author_uri' => 'http://sotomajor.org.ua',
    'description' => 'Allows to embed your smug-mug thumbnails into your web-site.',
    'type' => 1
);

// Plugin 'flags' signal the presence of optional capabilities to the core plugin loader.
// Use an appropriately OR-ed combination of these flags.
// The four high-order bits 0xf000 are available for this plugin's private use.
if (!defined('PLUGIN_HAS_PREFS')) define('PLUGIN_HAS_PREFS', 0x0001); // This plugin wants to receive "plugin_prefs.{$plugin['name']}" events
if (!defined('PLUGIN_LIFECYCLE_NOTIFY')) define('PLUGIN_LIFECYCLE_NOTIFY', 0x0002); // This plugin wants to receive "plugin_lifecycle.{$plugin['name']}" events

$plugin['flags'] = PLUGIN_HAS_PREFS | PLUGIN_LIFECYCLE_NOTIFY;

//include_once('zem_tpl.php');

# --- BEGIN PLUGIN CODE ---
global $txpcfg;

require_once($txpcfg['txpath'].'/include/str_smugplug_lib/phpSmug/phpSmug.php');
require_once($txpcfg['txpath'].'/include/str_smugplug_lib/str_SmugMug.php');
require_once($txpcfg['txpath'].'/include/str_smugplug_lib/str_SmugMugEntity.php');
require_once($txpcfg['txpath'].'/include/str_smugplug_lib/str_SmugMugAlbum.php');
require_once($txpcfg['txpath'].'/include/str_smugplug_lib/str_SmugMugImage.php');
require_once($txpcfg['txpath'].'/include/str_smugplug_lib/str_ImageDimensions.php');

function str_smug_image($params) {
    extract($params, EXTR_PREFIX_ALL, 'param');

    if (empty($param_id) || empty($param_key)) {
		return false;
	}

    $smugMug = new str_SmugMug();
    $image = $smugMug->getImageByIdAndKey($param_id, $param_key);
    if ($image instanceof str_SmugMugImage === false) {
        return '';
    }

    $imageSizeType = (isset($param_height) || isset($param_width)) ?
                        str_ImageDimensions::typeCustom : str_ImageDimensions::typeMedium;

    if (isset($param_linked) && $param_linked == 'false' || $param_linked == '0') {
        return $image->getImgTagHtml($imageSizeType, array($param_width, $param_height));
    } else {
        return $image->getLinkedImgTagHtml($imageSizeType, array($param_width, $param_height));
    }
}

/*
function str_smug_random_thumb($params) {
	extract($params, EXTR_PREFIX_ALL, 'param');

	if (empty($param_login)) {
		return false;
	}

	$smugMug = new str_SmugMug($param_login);
	return $smugMug->getRandomAlbum()->getRandomImage()->getTinyUrl();
}
*/
function str_smug_latest_albums_thumbs($params) {
	extract($params, EXTR_PREFIX_ALL, 'param');

	if (empty($param_login)) {
		return false;
	}

    if (empty($param_count) || ((int)$param_count > 0 === false)) {
        $param_count = 5;
    }

    $smugMug = new str_SmugMug($param_login);
    $albums = $smugMug->getAlbumList();

    $links = array();
    for($i = 0; $i < $param_count; $i++) {
        $links[] = $albums[$i]->getRandomImage()->getLinkedImgTagHtml(str_ImageDimensions::typeTiny);
    }

    return implode("\n", $links);
}

/**
 *
 * @param  $params
 * @param  $body
 * @return string
 */
function str_smug_image_fe($params, $body) {
    $apiUrl = 'http://api.smugmug.com/services/api/json/1.3.0/?Callback=?';

    $body = str_replace("\n", "", $body ? $body : get_pref("str_smugplug.entryPattern"));

    return "
    <script id=\"smugPlugScript\" type=\"text/javascript\">
            if (!smugPlugApi) {
                var smugPlugApi = new str_smugplug('".$params["apikey"]."', '".$apiUrl."');
            }

            smugPlugApi.str_smug_image('".$params['albumid']."', '".$params['albumkey']."', '".$params['imageid']."', function(data) {

            $('#smugPlugScript').before(tmpl('" . $body . "', data));
        });
    </script>
    ";
}

function str_smugplug_adm($event, $step) {

    $defaultEntryPattern = '<div class="smugMugArticleGallery"><a href="{imageHref}"><img src="{imageSrc}" /></a></div><p>{imageCaption}</p>';

    pagetop("SmugPlug", "Plugin for embedding SmugMug photos into textpattern articles");

    if (ps('saveConfig')) {
        $entryPattern = ps('entryPattern');
        $apiKey = ps('apiKey');

        set_pref("str_smugplug.entryPattern", ps('entryPattern'));
        set_pref("str_smugplug.apiKey", ps('apiKey'));
    } else {
        $entryPattern = get_pref("str_smugplug.entryPattern");
        $apiKey = get_pref("str_smugplug.apiKey");
    }

    echo <<<DATA
<div style="text-align:center;">
    <form action="index.php" method="post">
        <p>
        <label for="apiKey">SmugMug API key:</label>
        <input id="apiKey" name="apiKey" value="$apiKey" />
        </p>
        <p>
        <p>
        <label for="entryPattern">Pattern for image entry (don't use single quotes here!!!):</label>
        </p>
        <p>
        <textarea id="entryPattern" name="entryPattern" rows="10" cols="60">$entryPattern</textarea>
        </p>
        <p>
            <input type="hidden" name="event" value="str_smugplug_adm" >
            <input type="submit" name="saveConfig" value="Save" >
        </p>
    </form>
</div>
DATA;
}

if (txpinterface == 'admin') {
    add_privs('str_smugplug_adm', '1');
    register_tab('extensions', 'str_smugplug_adm', "SmugPlug");
    register_callback('str_smugplug_adm', 'str_smugplug_adm');
}

# --- END PLUGIN CODE ---